package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.*;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public ResponseEntity<Object> querySettings() throws InvocationTargetException, IllegalAccessException {
        User user = UserHolder.get();
        Settings settings = settingsApi.findSettingsByUid(user.getId());
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(user.getId());
            //settingsApi.save(settings);
        }
        SettingsVo settingsVo = new SettingsVo();
        Question question = questionApi.findByUid(user.getId());
        if (question != null) {
            settingsVo.setStrangerQuestion(question.getTxt());
            BeanUtils.copyProperties(settingsVo, settings);
        }
        settingsVo.setPhone(user.getMobile());
        System.out.println("settingsVo = " + settingsVo);
        return ResponseEntity.ok(settingsVo);
    }

    //保存通用设置业务
    public ResponseEntity<Object> notifiSettings(Settings settings) throws InvocationTargetException, IllegalAccessException {
        Long userId = UserHolder.getUserId();
        Settings settingsByUid = settingsApi.findSettingsByUid(userId);
        if (settingsByUid == null) {
            settingsByUid = new Settings();
            BeanUtils.copyProperties(settings, settingsByUid);
            settings.setUserId(userId);
            settingsApi.save(settings);
        } else {
            settings.setUserId(userId);
            settingsApi.updateSetitingsByUid(settings);
        }
        return ResponseEntity.ok(null);
    }

    //设置保存陌生人问题业务
    public ResponseEntity<Object> setQuestions(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUid(userId);
        if (question == null) {
            question = new Question(null,userId,content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.updateByUid(question);
        }
        return ResponseEntity.ok(null);
    }

    //查询黑名单业务
    public ResponseEntity<Object> blackList(Integer page, Integer pageSize) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //调用Api查询数据库
        IPage<UserInfo> iPage = blackListApi.findBlackList(page,pageSize,userId);
        //将结果注入PageResult对象
        PageResult pageResult = new PageResult(page,pageSize,(int)iPage.getTotal(),iPage.getRecords());
        return ResponseEntity.ok(pageResult);
    }

    //删除黑名单业务
    public ResponseEntity<Object> deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        BlackList blackList = new BlackList();
        blackList.setBlackUserID(blackUserId);
        blackList.setUserId(userId);
        blackListApi.deleteBlackList(blackList);
        return ResponseEntity.ok(null);
    }

    //修改手机号：发送验证码业务
    public ResponseEntity<Object> sendVerificationCode() {
        User user = UserHolder.get();
        String mobile = user.getMobile();
        userService.SendAuthCode(mobile);
        return ResponseEntity.ok(null);
    }

    //修改手机号：校验验证码业务
    public ResponseEntity<Object> checkVerificationCode(String verificationCode) {
        Map<String,Boolean> params = new HashMap<>();
        User user = UserHolder.get();
        String authCode = redisTemplate.opsForValue().get("SMS_KEY_" + user.getMobile());
        if(verificationCode == null || !verificationCode.equals(authCode)){
            params.put("verification", false);
            return ResponseEntity.status(200).body(params);
        }
        params.put("verification", true);
        return ResponseEntity.ok(params);
    }

    //修改手机号：修改号码业务
    public ResponseEntity<Object> updatePhone(String phone) {
        User user = UserHolder.get();
        user.setMobile(phone);
        return userService.updateMobile(user);
    }
}
